//
//  JCCMoneyViewController.swift
//  剂查查
//
//  Created by shoubin tao on 2024/12/3.
//  Copyright © 2024 shipin. All rights reserved.
//

import Foundation
import UIKit


class JCCApplyMoneyViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var bottomBtn: UIButton!
    
    @IBOutlet weak var imageW: NSLayoutConstraint!
    @IBOutlet weak var imageH: NSLayoutConstraint!
    @IBOutlet weak var wechatNumberTextField: UITextField!
    var qrcode_url:String = ""
    @IBOutlet weak var moneyLabel: UITextField!
    // 创建一个UIImagePickerController实例
    let imagePicker = UIImagePickerController()
    @IBAction func uploadAction(_ sender: Any) {
        
        showAlert()
    }
    @IBOutlet weak var imageView: UIImageView!
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        self.bottomBtn.layer.cornerRadius = 27.5
        self.bottomBtn.clipsToBounds = true
    }
    
    func showAlert () {
        // 创建UIAlertController并设置其样式为actionSheet
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        
        // 创建UIAlertAction来处理用户的选择
        let action1 = UIAlertAction(title: "拍摄", style: .default) { (action) in
            self.takePhoto()
        }
        let action2 = UIAlertAction(title: "从相册选择", style: .default) { (action) in
            self.choosePhoto()
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        
        // 将UIAlertAction添加到UIAlertController
        alert.addAction(action1)
        alert.addAction(action2)
        alert.addAction(cancelAction)
        
        // 显示UIAlertController
        present(alert, animated: true, completion: nil)
    }
    
    // 函数：选择照片
    func choosePhoto() {
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        present(imagePicker, animated: true)
    }
    
    // 函数：拍照获取图片
    func takePhoto() {
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        present(imagePicker, animated: true)
    }
    
    // MARK: - UIImagePickerControllerDelegate Methods
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
                picker.dismiss(animated: true)
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                    let image = info[.originalImage] as? UIImage
                    if (image != nil) {
                        // 获取选择的图片
                        EZLoadingActivity.show("正在上传图片...",disableUI: true)
                        JCCNet.uploadImage(image: image ?? UIImage(), completion: { (model, error) in
                            if(model?.isSuccess == true){
                                let dicts = model?.result as! [String:Any ]
                                self.imageH.constant = 100
                                self.imageW.constant = 100
                                self.imageView.image = image;    self.qrcode_url = dicts["url"] as? String ?? ""
                            }
                            EZLoadingActivity.Settings.SuccessText = model?.info ?? ""
                            EZLoadingActivity.hide(true, animated: false)
                            
                        })
                        
                    }
                }

        
        
    }
    @IBAction func applyAction(_ sender: Any) {
        EZLoadingActivity.show("正在申请...",disableUI: true)
        JCCNet.applyMoney(money: Int(self.moneyLabel?.text ?? "") ?? 0 , weixin: self.wechatNumberTextField.text ?? "", qrcode_url: self.qrcode_url) { (model, error) in
            EZLoadingActivity.Settings.SuccessText = model?.info ?? ""
            EZLoadingActivity.hide(true, animated: false)
            if(model?.isSuccess == true){
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // 用户取消选择图片时的处理
        picker.dismiss(animated: true)
    }
}
